//
//  YCCollectionCell.m
//  LOLBox
//
//  Created by mac on 15-3-26.
//  Copyright (c) 2015年 Honey. All rights reserved.
//

#import "YCCollectionCell.h"
#import "YCVideoModel.h"
#import "UIImageView+WebCache.h"

@interface YCCollectionCell ()
@property (weak, nonatomic) IBOutlet UIImageView *imgView;
@property (weak, nonatomic) IBOutlet UILabel *postTime;
@property (weak, nonatomic) IBOutlet UILabel *timeBg;

@property (weak, nonatomic) IBOutlet UILabel *name;

@end

@implementation YCCollectionCell


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
    }
    return self;
}

- (void)fillCellWithModel:(YCVideoModel *)model
{
    _imgView.layer.cornerRadius = 5;
    _imgView.clipsToBounds = YES;

    [_imgView sd_setImageWithURL:[NSURL URLWithString:model.img] placeholderImage:[UIImage imageNamed:@"noimage_xiangqing"]];
//    [self downloadImg:model.img];
    NSString *s = [NSString stringWithFormat:@"%@",model.time];
    _postTime.text = [self upTime:s];
    _name.text = model.name;
    
    //加边框
    CALayer *layer = [self.imgView layer];
    layer.borderColor = [[UIColor grayColor] CGColor];
    layer.borderWidth = 1.2f;
}

- (void)downloadImg:(NSString *)s
{
    dispatch_queue_t main = dispatch_get_main_queue();
    dispatch_queue_t global = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    //将任务添加到指定线程
    dispatch_async(global, ^{
        NSLog(@"开始下载");
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:s]];
        NSLog(@"下载完成");
        
        dispatch_async(main, ^{
            self.imgView.image = [UIImage imageWithData:data];
        });
    });
    
}


- (NSString *)upTime:(NSString *)str
{
    // 1.获得年月日
//    NSCalendar *calendar = [NSCalendar currentCalendar];
//    NSUInteger unitFlags = NSYearCalendarUnit| NSMonthCalendarUnit | NSDayCalendarUnit |NSHourCalendarUnit |NSMinuteCalendarUnit;
    
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[str floatValue]];
    
//    NSDateComponents *cmp1 = [calendar components:unitFlags fromDate:date];
//    NSDateComponents *cmp2 = [calendar components:unitFlags fromDate:[NSDate date]];
    
    // 2.格式化日期
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//    if ([cmp1 day] == [cmp2 day]) { // 今天
//        formatter.dateFormat = @"今天 HH:mm";
//    } else if ([cmp1 year] == [cmp2 year]) { // 今年
//        formatter.dateFormat = @"MM-dd HH:mm";
//    } else {
//        formatter.dateFormat = @"yyyy-MM-dd HH:mm";
//    }
    formatter.dateFormat = @"MM-dd";
    NSString *time = [formatter stringFromDate:date];
    
    // 3.显示日期
    return [NSString stringWithFormat:@"更新于%@", time];
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
